﻿using Abp.Authorization;
using Abp.Dependency;
using Abp.Events.Bus;
using Abp.UI;
using Abp.Web.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Web;

namespace ARchGL.Platform.Web.Validation
{
    public class AbpAuthorizeFilter : IAuthorizationFilter, ITransientDependency
    {
        private readonly IAuthorizationHelper _authorizationHelper;
        private readonly IErrorInfoBuilder _errorInfoBuilder;
        private readonly IEventBus _eventBus;

        public AbpAuthorizeFilter(
            IAuthorizationHelper authorizationHelper,
            IErrorInfoBuilder errorInfoBuilder,
            IEventBus eventBus)
        {
            _authorizationHelper = authorizationHelper;
            _errorInfoBuilder = errorInfoBuilder;
            _eventBus = eventBus;
        }

        public void OnAuthorization(AuthorizationFilterContext filterContext)
        {
            // throw new UserFriendlyException("AbpAuthorizeFilter Throw");

            if (filterContext == null)
            {
                throw new ArgumentNullException("filterContext");
            }
            //bool flag = filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) || filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true);
            //if (flag)
            //{
            //    return;
            //}
            var context = new FilterContext(filterContext);
            //权限验证

            //var user = filterContext.HttpContext.User.Identity as MyIdentity<AdminInfo>;

            //if (user == null || user.Datas == null || !user.IsAuthenticated)
            //{
            //    Dictionary<string, string> dicData = new Dictionary<string, string>();
            //    var qs = filterContext.HttpContext.Request.QueryString;
            //    for (int i = 0; i < qs.AllKeys.Length; i++)
            //    {
            //        dicData.Add(qs.AllKeys[i], qs[qs.AllKeys[i]]);
            //    }
            //    //url参数
            //    var par = Utils.FormatUrlParams(dicData);
            //    filterContext.Result = new RedirectResult(string.Format("/Admin/Sign?backUrl={1}", context.Area, HttpUtility.UrlEncode(string.Format("/{0}/{1}/{2}?{3}", context.Area, context.Controller, context.Action, par))));
            //    return;
            //}
        }
    }

    public class FilterContext
    {
        public FilterContext(AuthorizationFilterContext context)
        {
            this.Controller = context.RouteData.Values["controller"].ToString().ToLower();

            this.Action = context.RouteData.Values["action"].ToString().ToLower();

            var area = context.RouteData.DataTokens["area"];

            this.Area = (area ?? "").ToString().ToLower();
        }

        /// <summary>
        /// 动作
        /// </summary>
        public string Action { get; set; }

        /// <summary>
        /// 区域
        /// </summary>
        public string Area { get; set; }

        /// <summary>
        /// 控制器
        /// </summary>
        public string Controller { get; set; }
    }
}